export function getUrlParms(name) {
  var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if(r!=null)
    return unescape(r[2]);
  return null;
}
//Thu Jul 06 2017 00:00:00 GMT+0800 时间转化
export function dateFilter(){

  var d = new Date();
  var year = d.getFullYear();
  var month = d.getMonth() < 9 ? "0" + (d.getMonth() + 1) : "" + (d.getMonth() + 1);
  var day = d.getDate() < 10 ? "0" + d.getDate() : "" + d.getDate();
  var hour = d.getHours() < 10 ? "0" + d.getHours() : "" + d.getHours();
  var minutes = d.getMinutes() < 10 ? "0" + d.getMinutes() : "" + d.getMinutes();
  var seconds = d.getSeconds() < 10 ? "0" + d.getSeconds() : "" + d.getSeconds();
  return (year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + seconds);
}

export function timeFormat(time1) {
    // var time = new Date(time1).getTime();
    var time = new Date(time1.replace(/-/g, '/')).getTime();
    if (time > 0) {
      var result;
      time = parseInt(time);
      var minute = 1000 * 60;
      var hour = minute * 60;
      var day = hour * 24;
      var month = day * 30;
      var now = new Date().getTime();
      var diffValue = now - time;
      if (diffValue < 0) {
        return
      }
      var monthC = diffValue / month;
      var weekC = diffValue / (7 * day);
      var dayC = diffValue / day;
      var hourC = diffValue / hour;
      var minC = diffValue / minute;
      if (monthC >= 1) {
         if (monthC <= 12) {  //未超过一年  08-09 11:09:09
          //  result = "" + parseInt(monthC) + "月前";
          result =time1.slice(5,19)
         } else { //超过一年  2022-08-25 11:26:09
          //  result = "" + parseInt(monthC / 12) + "年前";
          result =time1
         }
      } else if (weekC >= 1) {
        // result = "" + parseInt(weekC) + "周前";
        result =time1.slice(5,19)
      } else if (dayC >= 1) {
        if(parseInt(dayC)==1){
          result = ""  + "昨天" +" " +time1.slice(11,19);
        }else{
          result =time1.slice(5,19)
        }
        // result = "" + parseInt(dayC) + "天前";
      } else if (hourC >= 1) {
        result = "" + parseInt(hourC) + "小时前";
      } else if (minC >= 1) {
        result = "" + parseInt(minC) + "分钟前";
      } else {
        result = "刚刚";
      }
      return result
    } else {
      return '';
    }
}




//秒数转化为时分秒
/**
 * 把秒数转化为天、时、分、秒
 * 参数value是秒数
 */
export function formatSeconds(value) {
  var secondTime = parseInt(value) // 秒
  var minuteTime = 0 // 分
  var hourTime = 0 // 小时
  var dayTime = 0 // 天
  var result = ''
  if (value < 60) {
    result = secondTime + '秒'
  } else {
    if (secondTime >= 60) { // 如果秒数大于60，将秒数转换成整数
      // 获取分钟，除以60取整数，得到整数分钟
      minuteTime = parseInt(secondTime / 60)
      // 获取秒数，秒数取佘，得到整数秒数
      secondTime = parseInt(secondTime % 60)
      // 如果分钟大于60，将分钟转换成小时
      if (minuteTime >= 60) {
        // 获取小时，获取分钟除以60，得到整数小时
        hourTime = parseInt(minuteTime / 60)
        // 获取小时后取佘的分，获取分钟除以60取佘的分
        minuteTime = parseInt(minuteTime % 60)
        if (hourTime >= 24) {
          // 获取天数， 获取小时除以24，得到整数天
          dayTime = parseInt(hourTime / 24)
          // 获取小时后取余小时，获取分钟除以24取余的分；
          hourTime = parseInt(hourTime % 24)
        }
      }
    }
    if (secondTime > 0) {
      secondTime = parseInt(secondTime) >= 10 ? secondTime : '0' + secondTime
      result = '' + secondTime + '秒'
    }
    if (minuteTime > 0) {
      minuteTime = parseInt(minuteTime) >= 10 ? minuteTime : '0' + minuteTime
      result = '' + minuteTime + '分' + result
    }
    if (hourTime > 0) {
      result = '' + parseInt(hourTime) + '小时' + result
    }
    if (dayTime > 0) {
      result = '' + parseInt(dayTime) + '天' + result
    }
  }
  return result
}
//秒转化为00:00:00
export function seconds(text){


  var hour = parseInt(text/ 3600);
  if (hour<10) {
    hour = "0" + hour;
  }
  var minute = parseInt((text % 3600) / 60);
  if (minute<10) {
    minute = "0" + minute;
  }
  var second = Math.ceil(text % 60);
  if (second<10) {
    second = "0" + second;
  }

  if(hour==0){
    var filetime =  minute + ":" + second;
  }else{
    var filetime = hour + ":" + minute + ":" + second;

  }
  return filetime
}

export function timeFormatDay(time1) {
  // var time = new Date(time1).getTime();
  var time = new Date(time1.replace(/-/g, '/')).getTime();
  if (time > 0) {
    var result;
    time = parseInt(time);
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue =time - now;
    if (diffValue < 0) {
      return
    }
    var monthC = diffValue / month;
    var weekC = diffValue / (7 * day);
    var dayC = diffValue / day;
    var hourC = diffValue / hour;
    var minC = diffValue / minute;
    if (monthC >= 1) {
      console.log(monthC>=1)
      result =time1
      /* if (monthC <= 12) {
         result = "" + parseInt(monthC) + "月前";
       } else {
         result = "" + parseInt(monthC / 12) + "年前";
       }*/
    }
    //  else if (weekC >= 1) {
    //   result ='还有'+ "" + parseInt(weekC) + "周";
    // } 
    else if (dayC >= 1) {
      result = '还有'+"" + parseInt(dayC) + "天";
    } else if (hourC >= 1) {
      result ='还有'+ "" + parseInt(hourC) + "小时";
    } else if (minC >= 1) {
      result = '还有'+ "" + parseInt(minC) + "分钟";
    } else {
      result = "";
    }
    return result
  } else {
    return '';
  }
}